#ifndef _HTTPSTREAM_HPP_
#define _HTTPSTREAM_HPP_

#include "jhdefs.hpp"
#include "net/HTTPSession.hpp"
#include "HTTPBasicStreamBuf.hpp"
#include "net/MemoryPool.hpp"

#include <istream>
#include <ostream>

NET_NAMESPACE_BEGIN


//
// CHTTPStreamBuf
//
class CHTTPStreamBuf : public HTTPBasicStreamBuf
{
public:
	typedef HTTPBasicStreamBuf::openmode openmode;

	CHTTPStreamBuf(CHTTPSession& session, openmode mode);
	~CHTTPStreamBuf();
	void close();

protected:
	int readFromDevice(char* buffer, std::streamsize length);
	int writeToDevice(const char* buffer, std::streamsize length);

private:
	CHTTPSession& m_session;
	openmode     m_mode;
};


//
// CHTTPIOS
//
class CHTTPIOS: public virtual std::ios
{
public:
	CHTTPIOS(CHTTPSession& session, CHTTPStreamBuf::openmode mode);
	~CHTTPIOS();
	CHTTPStreamBuf* rdbuf();

protected:
	CHTTPStreamBuf m_buf;
};


//
// CHTTPInputStream
//
class CHTTPInputStream : public CHTTPIOS, public std::istream
{
public:
	CHTTPInputStream(CHTTPSession& session);
	~CHTTPInputStream();

	void* operator new(std::size_t size);
	void operator delete(void* ptr);

private:
	static CMemoryPool m_pool;
};


//
// CHTTPOutputStream
//
class CHTTPOutputStream : public CHTTPIOS, public std::ostream
{
public:
	CHTTPOutputStream(CHTTPSession& session);
	~CHTTPOutputStream();

	void* operator new(std::size_t size);
	void operator delete(void* ptr);

private:
	static CMemoryPool m_pool;
};


NET_NAMESPACE_END

#endif
